package cn.bornsoft.mobileServer.logic;

import it.sauronsoftware.base64.Base64;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.apache.log4j.Logger;

import cn.bornsoft.mobileServer.db.BcNews;
import cn.bornsoft.mobileServer.db.BcNewsDAO;
import cn.bornsoft.mobileServer.db.BcNewsclass;
import cn.bornsoft.mobileServer.db.BcNewsclassDAO;
import cn.bornsoft.mobileServer.db.JkIndexFlash;
import cn.bornsoft.mobileServer.db.JkIndexFlashDAO;
import cn.bornsoft.mobileServer.db.JkKnowledgeTypeA;
import cn.bornsoft.mobileServer.db.JkKnowledgeTypeADAO;
import cn.bornsoft.mobileServer.db.JkNote;
import cn.bornsoft.mobileServer.db.JkNoteDAO;
import cn.bornsoft.utils.Html2Text;
import cn.bornsoft.utils.JsonDateValueProcessor;

/**
 * 负责处理业务逻辑的类
 * 
 * @author JONES
 * 
 */
public class ServiceLogic {

	static Logger log = Logger.getLogger(ServiceLogic.class);
	List list = null;
	Html2Text html2Text = new Html2Text();

	/**
	 * 根据一个或多个栏目ID，取出最新的max条新闻
	 * 
	 * @param num
	 * @param classid
	 * @return
	 */
	public String getNewsList(int max, int page, String classid) {
		String title = "";
		String content = "";
		BcNewsDAO newsDao = new BcNewsDAO();

		int i = 0;
		list = newsDao.findByClassID(max, page, classid);
		for (java.util.Iterator it = list.iterator(); it.hasNext(); i++) {
			BcNews bcnews = (BcNews) it.next();
			BcNews bcnewsmin = new BcNews();// 返回精简版的对象
			// 转标题 base64
			title = bcnews.getTitle();
			log.debug("title：" + title);
			title = Base64.encode(title, "GBK");
			// log.debug("btitle：" +title);
			bcnewsmin.setTitle(title);

			// 转正文 base64 去html和空格
			content = bcnews.getContent();
			content = html2Text.Html2Text(content).replaceAll("&nbsp;", "");
			if (content.length() > 28) {
				content = content.substring(0, 28);
				content = Base64.encode(content, "GBK");
			}
			log.debug("bcontent：" + content);
			bcnewsmin.setContent(content);

			bcnewsmin.setNewsid(bcnews.getNewsid());
			bcnewsmin.setPicpath(bcnews.getPicpath());
			bcnewsmin.setAdddate(bcnews.getAdddate());
			list.set(i, bcnewsmin);
		}
		// 把对象转为JSON

		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor("adddate",new JsonDateValueProcessor());
		cfg.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				if (name.equals("title") || name.equals("content")
						|| name.equals("newsid") || name.equals("picpath")
						|| name.equals("adddate")) {
					return false;
				} else {
					return true;
				}
			}
		});

		JSONArray js = JSONArray.fromObject(list, cfg);
		return js.toString();
	}

	public String getNewsListByNewsid(int max, int page, String newsids) {
		String title = "";
		String content = "";
		BcNewsDAO newsDao = new BcNewsDAO();

		int i = 0;
		list = newsDao.findByNewsids(max, page, newsids);
		for (java.util.Iterator it = list.iterator(); it.hasNext(); i++) {
			BcNews bcnews = (BcNews) it.next();
			BcNews bcnewsmin = new BcNews();// 返回精简版的对象
			// 转标题 base64
			title = bcnews.getTitle();
			log.debug("title：" + title);
			title = Base64.encode(title, "GBK");
			// log.debug("btitle：" +title);
			bcnewsmin.setTitle(title);

			// 转正文 base64 去html和空格
			content = bcnews.getContent();
			content = html2Text.Html2Text(content).replaceAll("&nbsp;", "");
			if (content.length() > 28) {
				content = content.substring(0, 28);
				content = Base64.encode(content, "GBK");
			}
			log.debug("bcontent：" + content);
			bcnewsmin.setContent(content);

			bcnewsmin.setNewsid(bcnews.getNewsid());
			bcnewsmin.setPicpath(bcnews.getPicpath());
			bcnewsmin.setAdddate(bcnews.getAdddate());
			list.set(i, bcnewsmin);
		}
		// 把对象转为JSON
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor("adddate",new JsonDateValueProcessor());
		cfg.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				if (name.equals("title") || name.equals("content")
						|| name.equals("newsid") || name.equals("picpath")
						|| name.equals("adddate")) {
					return false;
				} else {
					return true;
				}
			}
		});
		JSONArray js = JSONArray.fromObject(list, cfg);
		return js.toString();
	}

	/**
	 * 取得正文
	 * 
	 * @param newsid
	 * @return
	 */
	public String getNewsContent(String newsid) {
		BcNewsDAO newsDao = new BcNewsDAO();

		BcNews bcnews = newsDao.findById(newsid);
		BcNews bcnewsmin = new BcNews();// 返回精简版的对象
		String title = "";
		String content = "";
		bcnewsmin.setNewsid(newsid);
		title = Base64.encode(bcnews.getTitle(), "GBK");
		bcnewsmin.setTitle(title);
		// 去除正文中的链接
		content = Base64.encode(bcnews.getContent().replaceAll("<A[^>]+>", "")
				.replaceAll("</A>", "").replace(
						"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", ""), "GBK");
		bcnewsmin.setContent(content);

		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor("adddate",new JsonDateValueProcessor());
		cfg.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				if (name.equals("title") || name.equals("content")
						|| name.equals("newsid") || name.equals("picpath")
						|| name.equals("adddate")) {
					return false;
				} else {
					return true;
				}
			}
		});

		JSONObject js = JSONObject.fromObject(bcnewsmin, cfg);

		return js.toString();
	}

	/**
	 * 取得新闻栏目
	 * 
	 * @param classid
	 * @return
	 */

	public String getClasses(String classid) {
		String classcname = "";
		String content = "";
		BcNewsclassDAO newsclassDao = new BcNewsclassDAO();
		int i = 0;
		list = newsclassDao.findByParentid(classid);
		for (java.util.Iterator it = list.iterator(); it.hasNext(); i++) {
			BcNewsclass bcnewsclass = (BcNewsclass) it.next();
			BcNewsclass bcnewsclassmin = new BcNewsclass();
			// 转标题 base64
			classcname = Base64.encode(bcnewsclass.getClasscname(), "GBK");
			log.debug("classcname：" + classcname);
			// log.debug("btitle：" +title);
			bcnewsclassmin.setClasscname(classcname);
			bcnewsclassmin.setClassid(bcnewsclass.getClassid());
			list.set(i, bcnewsclassmin);
		}
		// 把对象转为JSON

		JsonConfig cfg = new JsonConfig();
		cfg.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				if (name.equals("classid") || name.equals("classcname")) {
					return false;
				} else {
					return true;
				}
			}
		});

		JSONArray js = JSONArray.fromObject(list, cfg);
		return js.toString();
	}

	/**
	 * 显示咨询列表
	 * 
	 * @param classid
	 * @return
	 */
	public String getNotes(String querytype, String searchKey, int max, int page) {
		String jkname = "";
		String tilte = "";
		String content = "";
		String replaycontent = "";
		JkNoteDAO jknnoteDao = new JkNoteDAO();
		int i = 0;
		String pname = "";
		if (querytype.equals("1")) {
			searchKey = Base64.decode(searchKey, "GBK");
			list = jknnoteDao.findByKey(searchKey, max, page);
		} else if (querytype.equals("0")) {
			list = jknnoteDao.findAll(max, page);
		}
		for (java.util.Iterator it = list.iterator(); it.hasNext(); i++) {
			JkNote jknote = (JkNote) it.next();
			// 转名字 base64
			jkname = jknote.getName();
			jkname = Base64.encode(jkname, "GBK");
			jknote.setName(jkname);
			log.debug("jkname：" + jkname);
			// 转标题 base64
			tilte = jknote.getTitle();
			tilte = Base64.encode(tilte, "GBK");
			jknote.setTitle(tilte);
			log.debug("tilte：" + tilte);
			// 转内容 base64
			content = jknote.getContent();
			content = Base64.encode(content, "GBK");
			jknote.setContent(content);
			log.debug("content：" + content);
			// 转回复 base64
			replaycontent = jknote.getReplaycontent();
			replaycontent = Base64.encode(replaycontent, "GBK");
			jknote.setReplaycontent(replaycontent);
			log.debug("replaycontent：" + replaycontent);

			list.set(i, jknote);
		}
		// 把对象转为JSON

		JsonConfig cfg = new JsonConfig();
		cfg.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				if (name.equals("jkname") || name.equals("tilte")
						|| name.equals("content")
						|| name.equals("replaycontent")) {
					return false;
				} else {
					return true;
				}
			}
		});

		JSONArray js = JSONArray.fromObject(list, cfg);
		return js.toString();
	}

	/**
	 * 取得幻灯数据
	 * 
	 * @return
	 */
	public String getSlide() {
		String title = "";
		String classname = "";
		JkIndexFlashDAO jkIndexFlashDao = new JkIndexFlashDAO();
		int i = 0;
		list = jkIndexFlashDao.findAll();
		for (java.util.Iterator it = list.iterator(); it.hasNext(); i++) {
			JkIndexFlash jkIndexFlash = (JkIndexFlash) it.next();
			title = Base64.encode(jkIndexFlash.getTitle(), "GBK");
			jkIndexFlash.setTitle(title);
			classname = Base64.encode(jkIndexFlash.getClassname(), "GBK");
			jkIndexFlash.setClassname(classname);
			list.set(i, jkIndexFlash);
		}
		// 把对象转为JSON
		JSONArray js = JSONArray.fromObject(list);
		return js.toString();
	}

	/**
	 * 保存咨询内容
	 * 
	 * @param jkNote
	 * @return
	 */
	public JkNote addNote(JkNote jkNote) {
		JkNoteDAO jkNoteDao = new JkNoteDAO();
		return jkNoteDao.merge(jkNote);
	}

	/**
	 * 取得疾病索引
	 * 
	 * @return
	 */
	public List getSike(char scode) {
		String id = "";
		String name = "";
		String itemid = "";
		Long ordernum;
		String code = "";
		JkKnowledgeTypeADAO jDao = new JkKnowledgeTypeADAO();
		int i = 0;
		list = jDao.findAll(scode);
		for (java.util.Iterator it = list.iterator(); it.hasNext(); i++) {
			JkKnowledgeTypeA jkta = (JkKnowledgeTypeA) it.next();
			name = Base64.encode(jkta.getName(), "GBK");
			jkta.setName(name);
			log.debug(name);
			list.set(i, jkta);
		}
		return list;
	}
}
